
/**
 * UserController
 *
 * @author glory.xu
 */


import { Post, Get, Prefix } from 'egg-shell-decorators';
import BaseController from '../base';


@Prefix('manage/user')
export default class UserController extends BaseController {


  @Get('/list')
  public async list() {
    const { ctx } = this;
    await ctx.render('manage/user/list.html');
  }

  @Get('/table')
  public async table({ request: { query } }) {
    const { ctx } = this;
    // limit,   // 每页记录数
    // offset, // 当前页索引
    const { limit = 10, offset = 0 } = query;
    const list = await ctx.service.user.getList({
      limit: parseInt(limit),
      offset: parseInt(offset),
    });
    this.ctx.body = list;
  }


  @Get('/addView')
  public async addView() {
    const { ctx } = this;
    await ctx.render('manage/user/add.html');
  }


  @Post('/add')
  public async add() {
    const { ctx } = this;
    const item = ctx.request.body;
    const result = ctx.service.user.create(item);
    this.success(result);
  }

  @Get('/editView')
  public async editView({ request: { query } }) {
    const { ctx } = this;
    const { id } = query;
    const item = await ctx.service.user.get(parseInt(id));
    await ctx.render('manage/user/edit.html', { mod: item });

  }

  @Post('/edit')
  public async edit() {
    const { ctx } = this;
    const item = ctx.request.body;
    const result = ctx.service.user.update(item, item.id);
    this.success(result);

  }

  @Get('/del')
  public async del({ request: { query } }) {
    const { ctx } = this;
    const { ids } = query;
    const array = ids.split(',');
    array.forEach(id => {
      ctx.service.user.destroy(parseInt(id));
    });
    this.success('');

  }

}

